package android.slc.extras.component.tree.ui

import android.app.Application
import android.slc.appbase.vm.AppBaseViewModel
import android.slc.extras.component.tree.entity.SlcTreeNav
import androidx.databinding.Observable
import androidx.databinding.ObservableField
import com.blankj.utilcode.util.CollectionUtils

/**
 * 树导航
 * @author slc
 * @date 2020-08-24 15:58
 */
class TreeNavVm(application: Application) : AppBaseViewModel(application) {
    private val slcTreeNavListOf = ObservableField<MutableList<SlcTreeNav>>(ArrayList())

    /**
     * 准备退出
     * 只是为了动画协调，没有其他意义
     */
    private val popOf = ObservableField<SlcTreeNav>()

    fun getSlcTreeNavListOf(): Observable {
        return slcTreeNavListOf
    }

    val slcTreeNavList: MutableList<SlcTreeNav>
        get() = slcTreeNavListOf.get()!!

    fun getPopOf(): Observable {
        return popOf
    }

    /**
     * 添加树导航
     *
     * @param slcTreeNav
     */
    fun addTreeNav(slcTreeNav: SlcTreeNav) {
        slcTreeNavListOf.get()!!.add(slcTreeNav)
        slcTreeNavListOf.notifyChange()
    }

    /**
     * 移除最后一个
     */
    fun removeLast() {
        val slcTreeNavList: MutableList<SlcTreeNav> = slcTreeNavList
        if (CollectionUtils.isNotEmpty(slcTreeNavList)) {
            slcTreeNavList.removeAt(slcTreeNavList.size - 1)
            slcTreeNavListOf.notifyChange()
        }
    }

    /**
     * 出栈
     *
     * @param currentPosition
     */
    fun pop(currentPosition: Int) {
        val slcTreeNavList: MutableList<SlcTreeNav> = slcTreeNavList
        val fireNavigateListSize = slcTreeNavList.size
        if (currentPosition == fireNavigateListSize - 1) {
            return
        }
        val startIndex = fireNavigateListSize - 1
        for (i in startIndex downTo currentPosition + 1) {
            val slcTreeNav = slcTreeNavList[i]
            if (i != startIndex) {
                slcTreeNav.isPopNoAnimator = true
            }
            popOf.set(slcTreeNav)
        }
    }
}